//
//  pickView.m
//  jiajiao
//
//  Created by apple on 16/3/31.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "pickView.h"
@interface pickView()<UIPickerViewDataSource,UIPickerViewDelegate>
@property(nonatomic,strong)NSArray *array;
@property(nonatomic,strong)NSArray *shiarray;
@property(nonatomic,strong)NSArray *quArray;
@property(nonatomic,strong)NSString *sheng;
@property(nonatomic,strong)NSString *shi;
@property(nonatomic,strong)NSString *qu;
@end
@implementation pickView
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpUI:frame];
    }
    return self;


}
-(void)setUpUI:(CGRect)frame
{
    UIPickerView *pick = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height-40*Kscr)];
    NSLog(@"%f",pick.frame.size.height);
    
    pick.dataSource = self;
    pick.delegate = self;
    [self addSubview:pick];
    NSString *path= [[NSBundle mainBundle]pathForResource:@"area.plist" ofType:nil];
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    self.array =array;
    self.shiarray = self.array[0][@"cities"];
    self.sheng = self.array[0][@"state"];
    self.shi = self.shiarray[0][@"city"];
    self.quArray = self.shiarray[0][@"areas"];
    if (self.quArray.count>0) {
        self.qu =self.quArray[0];

    }else
    {
    self.qu=@"";
    }
    
    NSLog(@"%f",pick.bottom);
    UIButton *butten = [[UIButton alloc]initWithFrame:CGRectMake(0, pick.bottom, frame.size.width, 40*Kscr)];
    [butten setTitle:@"确定" forState:UIControlStateNormal];
    [butten addTarget:self action:@selector(masurebtnClic) forControlEvents:UIControlEventTouchUpInside];
    butten.backgroundColor = [UIColor grayColor];
    [self addSubview:butten];
    
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component) {
        case 0:
           return  self.array.count;
            break;
        case 1:
            return  self.shiarray.count;
            break;
        case 2:
            return  self.quArray.count;
            break;

        default:
            break;
    }
    return 0;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    switch (component) {
        case 0:
            self.sheng = self.array[row][@"state"];
            self.shiarray =[self.array[row] objectForKey:@"cities"];
            [pickerView reloadComponent:1];
            [pickerView selectRow:0 inComponent:1 animated:YES];
           self.shi = self.shiarray[0][@"city"];
            self.quArray = [self.shiarray[0] objectForKey:@"areas"];
            [pickerView reloadComponent:2];
            [pickerView selectRow:0 inComponent:2 animated:YES];

            if (self.quArray.count>0) {
                self.qu =self.quArray[0];
            }
            else
            {
            self.qu= @"";
            }
            break;
        case 1:
            self.quArray = [self.shiarray[row] objectForKey:@"areas"];
            self.shi = self.shiarray[row][@"city"];
            [pickerView reloadComponent:2];
            [pickerView selectRow:0 inComponent:2 animated:YES];

            if (self.quArray.count>0) {
                self.qu =self.quArray[0];
            }else
            {
            self.qu =@"";
            }
            break;
        case 2:
            if (self.quArray.count>0) {
                self.qu =self.quArray[row];
            }else
            {
            self.qu =@"";
            }
            break;

        default:
            break;
    }
    
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (component) {
        case 0:
           return self.array[row][@"state"];

            break;
        case 1:
            return  self.shiarray[row][@"city"];
            break;
        case 2:
            return  self.quArray[row];
            break;
        default:
            break;
    }
    return nil;
}
-(void)masurebtnClic
{
    
    [self.deleget location:self.sheng shi:self.shi qu:self.qu];
    [self.deleget hidenpickerview];
    
    NSLog(@"%@______%@____%@",self.sheng,self.shi,self.qu);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
